<?php 

/***

LZRoster: A roster for World of Warcraft.
Copyright (C) 2007  Emmanuel Cron

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

***/

require_once dirname(__FILE__)."/LocaleContainer.class.php";

/**
 * Class used to easily manage Wowhead links
 * in the application.
 * 
 * @author Emmanuel Cron
 *
 */
class Wowhead {
	/** Instance of this class. */
	static private $instance;
	
	/** Item links to use with a given locale. */
	private $item_links_by_locale;
	
	/** Spell links to use with a given locale. */ 
	private $spell_links_by_locale;
	
	/**
	 * Initializes this class with  the item
	 * and spell links data.
	 */
	private function __construct()
	{
		$this->item_links_by_locale = unserialize(file_get_contents(dirname(__FILE__)."/../class.data/Wowhead.item_links_by_locale.serialize"));
		$this->spell_links_by_locale = unserialize(file_get_contents(dirname(__FILE__)."/../class.data/Wowhead.spell_links_by_locale.serialize"));
	}
	
	/**
	 * Returns a single instance of this class,
	 * or creates a new one if none exists.
	 * 
	 * @return an instance of this class
	 */
	static function GetInstance()
	{
		if (empty(self::$instance))
		{
			self::$instance = new Wowhead();
		}
		return self::$instance;
	}
	
	/**
	 * Returns the item link to use, depending
	 * on the given locale.
	 * 
	 * @param $locale the locale on which depend
	 * 		the link to return
	 * @return the item link of the given locale,
	 * 		or an default item link if it was not
	 * 		found
	 */
	function GetItemLinkByLocale($locale) {
		if (array_key_exists($locale, $this->item_links_by_locale)) {
			return $this->item_links_by_locale[$locale];
		} else {
			return $this->item_links_by_locale[LocaleContainer::GetInstance()->GetDefaultLocale()];
		}
	}
	
	/**
	 * Returns the spell link to use, depending
	 * on the given locale.
	 * 
	 * @param $locale the locale on which depend
	 * 		the link to return
	 * @return the spell link of the given locale,
	 * 		or an default item link if it was not
	 * 		found
	 */
	function GetSpellLinkByLocale($locale) {
		if (array_key_exists($locale, $this->spell_links_by_locale)) {
			return $this->spell_links_by_locale[$locale];
		} else {
			return $this->spell_links_by_locale[LocaleContainer::GetInstance()->GetDefaultLocale()];
		}
	}
}


?>